package com.xbs.dao.user;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.ParamSet;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.UserEntity;
/**
 * 关于用户的dao层
 * @author 余凯
 *
 */
public class UserDao extends BaseDao {
	//声明一个结果处理对象
	private RowDatas<UserEntity> user;
	/**
	 * 实例结果出来对象
	 */
	public UserDao(){
		user = new getUser();
	}
	/**
	 * 1.注册成功之后，向用户表插入数据
	 * @param user
	 * @return
	 */
	public int insertuser(UserEntity user){
		int row = 0;
		String sql = "insert into tb_user (user_name,user_idcard,user_telphone,user_password,user_paypassword) values(?,?,?,?,?)";
		row = updateExecuted(sql, new ParamSet(user.getUser_name(),user.getUser_idcard(),user.getUser_telphone(),user.getUser_password(),user.getUser_paypassword()));
		return row;
	}
	
	/**
	 * 2.查询出所有的用户
	 * @return
	 */
	public List<UserEntity> selAll(){
		String sql = "select * from tb_user";
		return queryList(sql, user);
	}
	
	/**
	 * 3.根据用户输入的手机号，获取该用户的密码
	 * @param tel
	 * @return
	 */
	public UserEntity selTel(String tel){
		String sql = "select * from tb_user where user_telphone = ?";
		return query(sql, new ParamSet(tel),user);
	}
	
	private class getUser implements RowDatas<UserEntity>{
		@Override
		public UserEntity getRowData(ResultSet rs) throws SQLException {
			UserEntity user = new UserEntity();
			user.setUser_name(rs.getString("user_name"));
			user.setUser_idcard(rs.getString("user_idcard"));
			user.setUser_telphone(rs.getString("user_telphone"));
			user.setUser_password(rs.getString("user_password"));
			user.setUser_paypassword(rs.getString("user_paypassword"));
			user.setUser_email(rs.getString("user_email"));
			return user;
		}
	}
	
	
}

